﻿@namespace Masa.Tsc.Web.Admin.Rcl.Pages.Apm
@inherits ApmComponentBase
@page "/apm/logs"
@page "/log/{TaskId}"
<PageTitle>@I18n.Apm("PageTitles.Log")</PageTitle>

<ApmSearchComponent Value="Search" IsLog @ref="ApmSearchComponent" ShowExceptError
                    ValueChanged="LoadAsync" />
<SDataTable Headers="headers"
            Stripe            
            FixedHeader
            ServerItemsLength="total"
            ItemsPerPage="defaultSize"
            MustSort
            Items="data"
            TItem="LogResponseDto"
            Loading="@isTableLoading"
            OnOptionsUpdate="OnTableOptionsChanged">
    <ItemColContent>
        @if (context.Header.Value == nameof(LogResponseDto.Timestamp))
        {
            <div class="d-flex justify-start">
                <SIcon OnClick="@(() => OpenAsync(context.Item))" Class="pr-2" Size="18" Tooltip="@I18n.T("Detail")">mdi-arrow-top-right-bottom-left</SIcon>
                @context.Item.Timestamp.UtcFormatLocal(CurrentTimeZone)
            </div>
        }
        else if (context.Header.Value == nameof(LogResponseDto.Body))
        {
            <MTooltip Top Context="tooltipContent">
                <ActivatorContent>
                    <div @attributes="@tooltipContent.Attrs" style="width:400px" class="text-truncate">
                        @context.Item.Body
                    </div>
                </ActivatorContent>
                <ChildContent>
                    <span>@context.Item.Body</span>
                </ChildContent>
            </MTooltip>
        }
        else if (context.Header.Value == "Resource.service.namespace")
        {
            @context.Item.Resource["service.namespace"]
        }
        else if (context.Header.Value == "Resource.service.name")
        {
            var service = @context.Item.Resource["service.name"]?.ToString();
            var text = GetService(service, Masa.Tsc.Web.Admin.Rcl.Pages.App.Index.GetService)?.AppDescription;
            if (string.IsNullOrEmpty(text))
                text = "无";
            <MTooltip Top Context="tooltipContent">
                <ActivatorContent>
                    <div @attributes="@tooltipContent.Attrs" style="width:400px" class="text-truncate">
                        @service
                    </div>
                </ActivatorContent>
                <ChildContent>
                    <span>@text</span>
                </ChildContent>
            </MTooltip>
        }
        else if (context.Header.Value == nameof(LogResponseDto.SeverityText))
        {
            @context.Item.SeverityText
        }
        else if (context.Header.Value == nameof(LogResponseDto.TraceId))
        {
            if (!string.IsNullOrEmpty(context.Item.TraceId))
            {
                context.Item.Attributes.TryGetValue("RequestPath", out var path);
                DateTime start = context.Item.Timestamp.AddHours(-6), end = context.Item.Timestamp.AddHours(6);
                var url = path?.ToString().Split('?')[0];
                <div>
                    <a style="text-decoration:none" href="/apm/endpoints/@(HttpUtility.UrlEncode(url)+ GetUrlParam(service: context.Item.Resource["service.name"].ToString(), env: context.Item.Resource["service.namespace"].ToString(),endpoint:url, start: start, end: end,traceId:context.Item.TraceId))" target="_blank">@context.Item.TraceId</a>
                </div>
            }
            else
            {
            }
        }
        else if (context.Header.Value == nameof(LogResponseDto.SpanId))
        {
            @context.Item.SpanId
        }
        else if (context.Header.Value == "Attributes.exception.type" && context.Item.Attributes.ContainsKey("exception.type"))
        {
            @context.Item.Attributes["exception.type"]
        }
    </ItemColContent>
</SDataTable>

<MCard Rounded=true Class="px-6 py-2" Style="border-top-left-radius: 0 !important; border-top-right-radius: 0 !important;">
    <SPagination Class="mt-2" @bind-Page="page" @bind-PageSize=defaultSize Total=total OnChange="OnPageChange" />
</MCard>

<style>
    .m-data-table .m-data-table__wrapper {
        height: calc(100vh - 300px)
    }
</style>


<ApmTraceView Value="current" @bind-Show="dialogShow" />